# coding:utf-8
import time

import requests
from lxml import etree
from tool import Excel

# 请求头
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36",
    "Cookie": "SINAGLOBAL=9622175689345.873.1563335524888; UM_distinctid=16f28e987da1e6-089d98baf0c27b-5a40201d-144000-16f28e987dbf1; _s_tentry=-; Apache=5344928383947.276.1592376373533; ULV=1592376374468:34:4:1:5344928383947.276.1592376373533:1591673967423; login_sid_t=b7f8b132cf3dd4497f28957412f2820a; cross_origin_proto=SSL; UOR=,,graph.qq.com; WBtopGlobal_register_version=fd6b3a12bb72ffed; SUB=_2A25z7bq3DeRhGeNH7FEW8CvNyD-IHXVQmqt_rDV8PUNbmtCOLW79kW9NSpgfTqI50z0zNZH2S0mQn6BcamzTUPSH; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WhzagMuy3NljbGwH0B2EzWs5JpX5KzhUgL.Fo-4S0eNeh-pe0e2dJLoIEXLxK-LBo5L12qLxKnLBoBLBo2LxKnLBoBLBo2LxKnLBoBLBo2LxKML1K.LB.Bt; SUHB=0auAl6Gg0qkS79; ALF=1623916135; SSOLoginState=1592380135; wvr=6; webim_unReadCount=%7B%22time%22%3A1592380141169%2C%22dm_pub_total%22%3A28%2C%22chat_group_client%22%3A0%2C%22chat_group_notice%22%3A0%2C%22allcountNum%22%3A58%2C%22msgbox%22%3A0%7D; WBStorage=42212210b087ca50|undefined"
}


def get_per_url():
    """
    获取每一页的url
    :return:
    """
    # 这里设置爬取十页
    for i in range(1, 30):
        url = 'https://s.weibo.com/weibo?q=%E9%AB%98%E8%80%83%E5%80%92%E8%AE%A1%E6%97%B6&wvr=6&b=1&Refer=SWeibo_box&page={}'.format(
            str(i))
        yield url


def get_per_response(url):
    """
    根据每一页的url，获取其每一项的内容，开始解析
    :param url:
    :return:
    """
    print(url)
    response = requests.get(url, headers=headers)
    root = etree.HTML(response.text)
    articles = root.xpath('//div[@class="card-wrap"]')
    for article in articles:
        # 动态内容
        article_content = article.xpath('div/div/div/p[@class="txt"]/text()')
        article_content = "".join(article_content).replace(' ', '').replace('\n', '').strip()

        # 用户名称
        user_name = article.xpath('div//div[@class="content"]/div/div/a/@nick-name')
        user_name = user_name[0] if user_name else "无用户名"

        # 收藏、转发、评论、点赞人数
        # 发布时间
        try:
            collect = article.xpath('div//div[@class="card-act"]/ul/li[1]/a/text()')
            collect = 0 if collect[0].strip() == '收藏' else collect[0].strip().split(' ')[1]
            repeat = article.xpath('div//div[@class="card-act"]/ul/li[2]/a/text()')
            repeat = 0 if repeat[0].strip() == '转发' else repeat[0].strip().split(' ')[1]
            comment = article.xpath('div//div[@class="card-act"]/ul/li[3]/a/text()')
            comment = 0 if comment[0].strip() == '评论' else comment[0].strip().split(' ')[1]
            support = article.xpath('div//div[@class="card-act"]/ul/li[4]/a/em/text()')
            support = 0 if not support else support[0]
            # 时间
            publish_time = article.xpath('div/div/div/p[@class="from"]/a/text()')
            publish_time = publish_time[0].strip()
            # tool - 获取具体的时间
            # 秒
            try:
                if "秒" in publish_time:
                    publish_time = publish_time.replace("秒前", '').strip()
                    # 拿当前时间戳减去多少秒，然后转化成对应的日期
                    timestamp = time.time()
                    timestamp -= int(publish_time)
                    publish_time = time.strftime("%Y-%m-%d %H:%M", time.localtime(timestamp))
                elif "分钟" in publish_time:
                    try:
                        publish_time = publish_time.replace("分钟前", '').strip().split(' ')[0]
                    except Exception as e:
                        publish_time = publish_time.replace("分钟前", '').strip()
                    timestamp = time.time()
                    timestamp -= int(publish_time) * 60
                    publish_time = time.strftime("%Y-%m-%d %H:%M", time.localtime(timestamp))
                elif "今天" in publish_time:
                    now_time = time.localtime()
                    publish_time = publish_time.replace("今天", '').strip()
                    localtime = time.strftime("%Y-%m-%d", now_time)
                    publish_time = localtime + ' ' + publish_time
                if user_name != "无用户名":
                    print(user_name, publish_time, article_content, collect, repeat, comment, support)
                    yield user_name, publish_time, article_content, collect, repeat, comment, support
            except ValueError as e:
                pass
        except IndexError as e:
            pass


if __name__ == '__main__':
    # 创建excel对象
    xcl = Excel()
    # row从1开始，每写入xls一行数据，row += 1
    row = 1
    for i in get_per_url():
        # 返回一个元组，这个元组包括每一条动态的数据
        for per_data in get_per_response(i):
            xcl.write_data(row, per_data)
            row += 1
        xcl.save_excel_data()
